global g_root "C:\Users\Hongye Guo\Dropbox (Personal)\Density\Code\Production\Submission"

use "$g_root\Data\gfd_clean.dta", clear
gen temp = ret if country == "US"
sort ym country
by ym: egen us_ret = max(temp)
drop temp

drop if ym<ym(1900, 1)
local l_window = 8
gen yq = qofd(dofm(ym))
sum yq
local l_start = `r(min)' + `l_window'-1
local l_end = `r(max)'
levelsof country 
gen beta_us_ret = .
foreach l_var in `r(levels)'{
	forvalues l_year = `l_start'/`l_end'{
		capture reg ret us_ret if country == "`l_var'" & yq<=`l_year' & yq>`l_year'-`l_window'
		if _rc == 0{
			mat A = e(b)
			replace beta_us_ret = A[1,1] if country == "`l_var'" & yq==`l_year'+1 & `e(N)'>=18
		}
	}
}
drop yq

* Table C2: Panel A
* Panel B uncomment the following line
*replace ret = ret - beta_us_ret*us_ret - (1-beta_us_ret)*rf

rename ret mportret
encode country, gen(country_id)
tsset country_id ym
forvalues i = 1/12{
	gen mportret_l`i'm = L`i'.mportret
}

gen mportret_f1m = F1.mportret
gen ym_f1m = F1.ym
rename mportret mportret_l0m  
gen month = month(dofm(ym_f1m))

forvalues i = 1/4{
	local j = (`i'-1)*3 
	local jp1 = `j' + 1
	local jp2 = `j' + 2
	local jp3 = `j' + 3
	gen mportret_prevq2_l`i'q = mportret_l`j'm + mportret_l`jp1'm if mod(month,3)==0
	replace mportret_prevq2_l`i'q = mportret_l`jp1'm + mportret_l`jp2'm if mod(month,3)==1
	replace mportret_prevq2_l`i'q = mportret_l`jp2'm + mportret_l`jp3'm if mod(month,3)==2
}

gen am2_flag = mod(month, 3)==1 | mod(month, 3)==2
gen mportret_prevq2_t4q = mportret_prevq2_l1q + mportret_prevq2_l2q + mportret_prevq2_l3q + mportret_prevq2_l4q 
gen mportret_prevq2_t4q_x_am2_flag = mportret_prevq2_t4q * am2_flag 
forvalues j = 1/4{
	gen mportret_prevq2_l`j'q_x_am2_flag = am2_flag * mportret_prevq2_l`j'q 
}

local col_count = 0
local title_list

clear matrix
local col_count = 0
local title_list	
reg mportret_f1m mportret_prevq2_t4q if country != "US" & ym>=ym(1926, 1) , cluster(ym)	
local col_count = `col_count' + 1
estimates store sep_`col_count'	
reg mportret_f1m mportret_prevq2_t4q if country != "US" & ym>=ym(1926, 1) & am2_flag, cluster(ym)	
local col_count = `col_count' + 1
estimates store sep_`col_count'	
reg mportret_f1m mportret_prevq2_t4q if country != "US" & ym>=ym(1926, 1) & !am2_flag, cluster(ym)	
local col_count = `col_count' + 1
estimates store sep_`col_count'	
reg mportret_f1m mportret_prevq2_t4q mportret_prevq2_t4q_x_am2_flag am2_flag if country != "US" & ym>=ym(1926, 1) , cluster(ym)	
local col_count = `col_count' + 1
estimates store sep_`col_count'	
reg mportret_f1m mportret_prevq2_t4q mportret_prevq2_t4q_x_am2_flag am2_flag if country != "US" & ym>=ym(1947, 1) , cluster(ym)	
local col_count = `col_count' + 1
estimates store sep_`col_count'	
reg mportret_f1m mportret_prevq2_t4q mportret_prevq2_t4q_x_am2_flag am2_flag if country != "US" & ym<ym(1974, 1) & ym>=ym(1926, 1), cluster(ym)	
local col_count = `col_count' + 1
estimates store sep_`col_count'	
reg mportret_f1m mportret_prevq2_t4q mportret_prevq2_t4q_x_am2_flag am2_flag if country != "US" & ym>=ym(1974, 1), cluster(ym)	
local col_count = `col_count' + 1
estimates store sep_`col_count'	

local l_date : display %tdCYND date(c(current_date), "DMY")
local l_time = substr(c(current_time), 1, 2) + substr(c(current_time), 4, 2)
esttab sep_* using "$g_root/Output/Global_`l_date'`l_time'.csv", star(* 0.1  ** 0.05 *** 0.01) b(3) t(2) r2 br noomit replace 
